<template>
  <view>
    <biaotou title="当事人信息"></biaotou>
    <inputForm v-model="form" ref="form" :rules="rules" :list="list" v-on="$listeners">
    </inputForm>
  </view>
</template>

<script>
import {sexTypeJson, trueFalseJson, trueFalseJson2} from "../mock/mockJson";
import Biaotou from "./biaotou.vue";
import luchangtianjia from "./luchantianjia.vue";
import inputForm from "@/components/inputForm.vue";
import {selectDicByCode} from "../../../api/lupeiwenshu";
import {getAgeByIdCard, regexIdCard} from "@/utils/fun";

export default {
  components: {luchangtianjia, Biaotou, inputForm},
  data() {
    return {
      clientJobSelectShow: false,
      form: {
        clientName: '',
        clientGender: '1',
        clientIdcard: '身份证',
        clientIdcardNum: '',
        clientAge: '',
        clientJob: '司机',
        clientTel: '',
        clientAddress: '',
        zipcode: '',
        clientComName: '',
        clientComAddr: '',
        isDriver: '1',
        isOwner: '1',
        carOwner:'',
        clientRelation: '',
        isScene: '1'
      },
      list: [
        {
          label: '姓名：',
          prop: 'clientName',
        },
        {
          label: '性别：',
          prop: 'clientGender',
          type: 'select',
          dicData: sexTypeJson,
        },
        {
          label: '证件类型：',
          prop: 'clientIdcard',
          type: 'select',
          props: {
            label: 'name',
            value: 'name'
          },
          dicData: []
        },
        {
          label: '证件号：',
          prop: 'clientIdcardNum',
        },
        {
          label: '与案件关系：',
          prop: 'clientRelation',
          labelWidth: 220
        },
        {
          label: '年龄：',
          prop: 'clientAge',
        },
        {
          label: "职业：",
          prop: 'clientJob',
          type: 'select',
          props: {
            label: 'name',
            value: 'name'
          },
          dicData: []
        },
        {
          label: '电话：',
          prop: 'clientTel',
        }, {
          label: '住址：',
          prop: 'clientAddress',
          type: 'textarea',
        }, {
          label: '邮编：',
          prop: 'zipcode',
        }, {
          label: '单位：',
          prop: 'clientComName',
        }, {
          label: '单位地址：',
          prop: 'clientComAddr',
        }, {
          label: '是否是驾驶员：',
          prop: 'isDriver',
          type: 'select',
          labelWidth: 280,
          dicData: trueFalseJson,
        }, {
          label: '是否车辆所有人：',
          prop: 'isOwner',
          type: 'select',
          labelWidth: 280,
          dicData: trueFalseJson,
        }, {
          label: '车辆所有人：',
          prop: 'carOwner',
          labelWidth: 280,
          hidden: true,
        }, {
          label: '是否在现场：',
          prop: 'isScene',
          type: 'select',
          labelWidth: 280,
          dicData: trueFalseJson2,
        }
      ],
      rules: {

      }
    };
  },
  watch: {
    'form.clientIdcardNum'(val) {
        // regexIdCard(val)
        try {
          this.form.clientAge = getAgeByIdCard(val)
        }catch{}
    },
    'form.isOwner'(val) {
      let show = true
      if(val === '0'){
        show = false
      }
      this.list.find(item => item.label === '车辆所有人：').hidden = show;
    }
  },
  methods: {
    async initSelectJson() {
      this.list.find(item => item.label === '证件类型：').dicData = (await selectDicByCode('zjlx')).data;
      this.list.find(item => item.label === '职业：').dicData = (await selectDicByCode('job')).data;
    },
  },
  // 必须要在onReady生命周期，因为onLoad生命周期组件可能尚未创建完毕
  mounted() {
    this.initSelectJson()

    // this.getUserList();

    uni.getStorage({
      key: 'userInfo',
      success: (res) => {
        if (res.data) {
          this.form.fillPerson = res.data.realName
        }
      }
    });
  },
};
</script>
